/*************************************
* Created by WebStorm
* Author : 艾尼亚尔·毛拉吾提  
* QQ : 524155792  WeChat : ainiyar0124
* Created Time :2025/1/21 14:47
Desc :
**************************************/

import Cookies from 'js-cookie';
import {Config} from '@/config';

const TOKEN_KEY: string = Config.TOKEN_KEY;
//定义token过期时间
const timeKey: string = Config.LOCAL_STORAGE_key + '_expireTime'

/**
 * 存储token
 * @param token token
 * @param expireTime 过期时间 从后端过来
 */
export function setToken(token: string, expireTime: number): void {
    //保存时间为8小时
    const inFifteenMinutes = new Date(expireTime);
    Cookies.set(TOKEN_KEY, token, {expires: inFifteenMinutes});
}

/**
 * 获取token
 */
export function getToken(): string {
    const token = Cookies.get(TOKEN_KEY);
    if (token) return token;
    else return "";
}

/**
 * 删除token
 */
export function removeToken(): void {
    Cookies.remove(TOKEN_KEY); // fail!
}

/**
 * 清空sessionStorage
 */
export function clearStorage(): void {
    return sessionStorage.clear();
}

/**
 * 设置token过期时间
 * @param time
 */
export function setTokenTime(time: number): void {
    return sessionStorage.setItem(timeKey, String(time));
}

/**
 * 获取token过期时间
 */
export function getTokenTime(): string | null {
    return sessionStorage.getItem(timeKey);
}

/**
 * 清空token过期时间
 */
export function removeTokenTime(): void {
    return sessionStorage.setItem(timeKey, String(0));
}
/**
 * 上传文件头部
 */
export function uploadFileHeader ():{Authorization:string} {
    return {Authorization:Config.TOKEN_PREFIX+getToken()}
}